package com.longtengyuyun.socke_demo.sock;

import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author ZhaoYunLong
 * @date 2022/4/14
 * @description
 */
public class SocketTwoServer {


    public static void main(String[] args) {

        try {
            int port = 5555;
            ServerSocket serverSocket = new ServerSocket(port);

            System.out.println("等待连接的到来");

            Socket accept = serverSocket.accept();
            InputStream inputStream = accept.getInputStream();

            byte[] bytes = new byte[1024];
            int len;
            byte[] totalBytes = new byte[]{};
            int totalLength = 0;
            while ((len = inputStream.read(bytes)) != -1) {
//1. 将读取的数据和上一次遗留的数据拼起来
                int tempLength = totalLength;
                totalLength = len + totalLength;
                byte[] tempBytes = totalBytes;
                totalBytes = new byte[totalLength];
                System.arraycopy(tempBytes, 0, totalBytes, 0, tempLength);
                System.arraycopy(bytes, 0, totalBytes, tempLength, len);
                while (totalLength > 4) {
                    byte[] lengthBytes = new byte[4];
                    System.arraycopy(totalBytes, 0, lengthBytes, 0, lengthBytes.length);
                    int contentLength = Utils.bytes2Int(lengthBytes);
                    //2. 如果剩下数据小于数据头标的长度，则出现拆包，再次获取数据连接
                    if (totalLength < contentLength + 4) {
                        break;
                    }
                    //3. 将数据头标的指定长度的数据取出则为应用数据
                    byte[] contentBytes = new byte[contentLength];
                    System.arraycopy(totalBytes, 4, contentBytes, 0, contentLength);
                    //注意指定编码格式，发送方和接收方一定要统一，建议使用UTF-8
                    String content = new String(contentBytes, "UTF-8");
                    System.out.println("contentLength = " + contentLength + ", content: " + content);
                    //4. 去掉已读取的数据
                    totalLength -= (4 + contentLength);
                    byte[] leftBytes = new byte[totalLength];
                    System.arraycopy(totalBytes, 4 + contentLength, leftBytes, 0, totalLength);
                    totalBytes = leftBytes;
                }

            }

            inputStream.close();
            accept.close();
            serverSocket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
